/******************************************************************
 * Created by: Goh Chee Hong                                       
 * Date of Creation: 1/Jan/2009     
 * 
 *                                                                
 * Description: The form that will prompt for a response from the user
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using Pbdms.BusinessLogic.FileTransfer;
using Pbdms.NetworkDealer;
using Pbdms.Common.DataObject;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Tools
{
    public partial class FrmAcceptFile : newForms
    {
        string fileName;
        long size;
        string keyid;
        NetworkDealer.AuthTcpClient authTcpClient;
        public FrmAcceptFile()
        {
            InitializeComponent();
          
            Pbdms.Common.Global.paintBackGround(this);
        }
        public FrmAcceptFile(object sender, NetworkMessage message)
        {
            //spilt into two parameters

            InitializeComponent();
            this.authTcpClient = (NetworkDealer.AuthTcpClient)sender;

            int pos= message.MessageContent.LastIndexOf(',');
            //file name and file size
            fileName = message.MessageContent.Substring(0, pos);
            size = long.Parse(message.MessageContent.Substring(pos+1, message.MessageContent.Length - pos-1));
            keyid = message.SenderKeyID;
            Buddy buddy = Program.friendControl.getAFriendByKeyID(message.SenderKeyID);
            this.lblInfo.Text = buddy.Name + " wants to send you a file " + fileName + " (" + size + ")";
        }

        public override void changeName(string keyID, string oldName, string newName)
        {
            if (this.keyid.Equals(keyID))
            {
                this.lblInfo.Text = newName+ " wants to send you a file " + fileName + " (" + size + ")";
            }
        }
        /// <summary>
        /// accept button event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAccept_Click(object sender, EventArgs e)
        {
            
            string filelocation=Common.Global.getReceivedFileFolder()+fileName;
            int i = 1;
            //same filename found
            while(File.Exists(filelocation)){

                int pos = fileName.LastIndexOf('.');
                if (pos != -1)
                {

                }
                else
                {
                    pos = fileName.Length;
                }
                filelocation = Common.Global.getReceivedFileFolder() + fileName.Insert(pos, "_" + i.ToString());
                i++;
            }
            //get the buddy information from the share control
            Common.DataObject.Buddy buddy= Program.friendControl.getAFriendByKeyID(keyid);
            FileTransferReceiverGui gui = new FileTransferReceiverGui(keyid, buddy.Name, filelocation,size,authTcpClient, Program.networkCtrller);
            Program.mainForm.frmFileTransfer.addControl(gui);
            this.Close();
        }
        /// <summary>
        /// save button event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            saveFileDia.FileName = fileName;
            DialogResult dlgResult = saveFileDia.ShowDialog();
            string filelocation = "";
            if (dlgResult == DialogResult.OK)
            {
                filelocation = saveFileDia.FileName;
                Common.DataObject.Buddy buddy = Program.friendControl.getAFriendByKeyID(keyid);
                FileTransferReceiverGui gui = new FileTransferReceiverGui(keyid, buddy.Name, filelocation, size, authTcpClient, Program.networkCtrller);
                Program.mainForm.frmFileTransfer.addControl(gui);
                this.Close();
                
            }
        }
        /// <summary>
        /// cancel button event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            NetworkMessage message = new NetworkMessage();
            message.TypeOfMessage = NetworkDealer.NetworkDealer.TRANSFER_FILE_REQUEST_REJECTED;
            try
            {
                authTcpClient.sendMessage(message);
            }
            catch
            {

            }
            
            this.Close();
        }
    }
}